package com.ossjk.oa.accounting.service.impl;

import com.ossjk.core.util.CommonMethod;
import com.ossjk.core.util.CommonUtil;
import com.ossjk.core.util.JwtTokenUtil;
import com.ossjk.core.vo.PagePreNextVo;
import com.ossjk.oa.accounting.entity.CwCapitals;
import com.ossjk.oa.accounting.service.ICwCapitalsService;
import com.ossjk.oa.accounting.vo.CwReceivablesVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.ossjk.oa.accounting.entity.CwReceivables;
import com.ossjk.oa.accounting.mapper.CwReceivablesMapper;
import com.ossjk.oa.accounting.service.ICwReceivablesService;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author chair
 * @since 2020-05-14
 */
@Service
public class CwReceivablesServiceImpl extends ServiceImpl<CwReceivablesMapper, CwReceivables> implements ICwReceivablesService {
	@Autowired
	private ICwCapitalsService iCwCapitalsService;
	@Autowired
	protected JwtTokenUtil jwtTokenUtil;
	@Override
	public Page selectVoPage(Page page, Wrapper wrapper) {
		// TODO Auto-generated method stub
		return null;
	}
	@Override
	public PagePreNextVo selectPreOrNextByMth(String midMth) {
		PagePreNextVo vo = new PagePreNextVo();

		vo.setPre(this.baseMapper.selectPreMth(midMth));
		vo.setNext(this.baseMapper.selectNextMth(midMth));
		vo.setCurrent(midMth);
		//取得6个月以前

		vo.setSelectAll(this.baseMapper.selectMax12Mth(CommonMethod.getxxMthBefore(midMth, -6)));

		return vo;
	}
	protected HttpServletRequest getRequest() {
		HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();

		return request;
	}
	@Override
	public List<CwReceivablesVo> selectCapitalsByMth(Wrapper wrapper) {
		return baseMapper.selectAllByMth(wrapper);
	}
	protected String getCurrnetUserId() {
		return jwtTokenUtil.getUserId(jwtTokenUtil.getToken(getRequest()));
	}
	@Override
	public int countByMth(String mth) {
		return baseMapper.countByMth(mth).intValue();
	}
	public boolean cwUpdateReceivables(CwReceivables cw_receivables, String cptname, String cpttype){
		CwReceivables cwReceivables = selectById(cw_receivables.getId());
		BigDecimal add = null;
		if (CommonUtil.isBlank(cwReceivables.getReceived())) {
			add = cw_receivables.getReceived();
		} else {
			add = cwReceivables.getReceived().add(cw_receivables.getReceived());
		}
		// a = -1,表示bd1小于bd2；
		// a = 0,表示bd1等于bd2；
		// a = 1,表示bd1大于bd2；
		//状态[0：待收，1,:已收，2.部分收取；3，坏账]
		BigDecimal rec = cw_receivables.getReceived();
		cw_receivables.setReceived(add);
		if (cwReceivables.getReceivables().compareTo(cw_receivables.getReceived()) == -1) {
			cw_receivables.setFlag(1);
			cw_receivables.setRecemp(getCurrnetUserId());

		}else if (cwReceivables.getReceivables().compareTo(cw_receivables.getReceived()) == 0){
			cw_receivables.setFlag(1);
			cw_receivables.setRecemp(getCurrnetUserId());
		}else if(cwReceivables.getReceivables().compareTo(cw_receivables.getReceived()) == 1){
			cw_receivables.setFlag(2);
		}


		if (updateById(cw_receivables)) {
			CwCapitals cwCapitals = new CwCapitals();
			cwCapitals.setCptname(cptname);
			cwCapitals.setCptdate(cw_receivables.getActecDate());
			cwCapitals.setCptacount(rec);
			cwCapitals.setTypec("应收款项");
			cwCapitals.setFlag(1);
			cwCapitals.setCpttype(cpttype);
			cwCapitals.setDescb(cw_receivables.getDescb());
			cwCapitals.setReceivablesid(cw_receivables.getId());
			iCwCapitalsService.insert(cwCapitals);
			return true;
		} else {
			return false;
		}
	}

}
